/**
 * 安全距离工具类
 * 用于处理不同设备的安全区域适配
 */

/**
 * 获取系统信息并计算安全距离
 */
export const getSafeAreaInfo = () => {
  const systemInfo = uni.getSystemInfoSync()
  
  return {
    // 状态栏高度
    statusBarHeight: systemInfo.statusBarHeight || 0,
    
    // 屏幕宽高
    screenWidth: systemInfo.screenWidth,
    screenHeight: systemInfo.screenHeight,
    
    // 安全区域
    safeArea: systemInfo.safeArea || {
      top: systemInfo.statusBarHeight || 0,
      bottom: systemInfo.screenHeight,
      left: 0,
      right: systemInfo.screenWidth
    },
    
    // 安全区域内边距
    safeAreaInsets: systemInfo.safeAreaInsets || {
      top: systemInfo.statusBarHeight || 0,
      bottom: 0,
      left: 0,
      right: 0
    }
  }
}

/**
 * 获取安全距离的CSS变量字符串
 */
export const getSafeAreaCSSVars = () => {
  const info = getSafeAreaInfo()
  
  return {
    '--safe-area-inset-top': `${info.safeAreaInsets.top}px`,
    '--safe-area-inset-bottom': `${info.safeAreaInsets.bottom}px`,
    '--safe-area-inset-left': `${info.safeAreaInsets.left}px`,
    '--safe-area-inset-right': `${info.safeAreaInsets.right}px`,
    '--status-bar-height': `${info.statusBarHeight}px`
  }
}

/**
 * 页面容器的安全距离样式类
 */
export const safeAreaClasses = {
  // 全屏容器 - 适用于需要全屏显示的页面
  fullscreen: `
    height: 100vh;
    min-height: 100vh;
    box-sizing: border-box;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  `,
  
  // 带内边距容器 - 适用于有内边距的页面
  withPadding: (padding: string = '24rpx') => `
    height: 100vh;
    min-height: 100vh;
    box-sizing: border-box;
    padding-top: calc(env(safe-area-inset-top) + ${padding});
    padding-bottom: calc(env(safe-area-inset-bottom) + ${padding});
    padding-left: calc(env(safe-area-inset-left) + ${padding});
    padding-right: calc(env(safe-area-inset-right) + ${padding});
  `,
  
  // 顶部安全距离 - 只处理顶部
  topOnly: `
    padding-top: env(safe-area-inset-top);
  `,
  
  // 底部安全距离 - 只处理底部
  bottomOnly: `
    padding-bottom: env(safe-area-inset-bottom);
  `,
  
  // 固定底部按钮 - 适用于底部固定按钮
  fixedBottom: (padding: string = '20rpx') => `
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    box-sizing: border-box;
    padding-bottom: calc(${padding} + env(safe-area-inset-bottom));
    padding-left: calc(${padding} + env(safe-area-inset-left));
    padding-right: calc(${padding} + env(safe-area-inset-right));
  `
}

/**
 * 计算带安全距离的高度
 */
export const calcHeightWithSafeArea = (baseHeight: string) => {
  return `calc(${baseHeight} + env(safe-area-inset-bottom))`
}

/**
 * 计算带安全距离的内边距
 */
export const calcPaddingWithSafeArea = (
  basePadding: string,
  direction: 'top' | 'bottom' | 'left' | 'right' | 'all' = 'all'
) => {
  const safeAreaVar = `env(safe-area-inset-${direction})`
  
  if (direction === 'all') {
    return {
      paddingTop: `calc(${basePadding} + env(safe-area-inset-top))`,
      paddingBottom: `calc(${basePadding} + env(safe-area-inset-bottom))`,
      paddingLeft: `calc(${basePadding} + env(safe-area-inset-left))`,
      paddingRight: `calc(${basePadding} + env(safe-area-inset-right))`
    }
  }
  
  return `calc(${basePadding} + ${safeAreaVar})`
}

/**
 * 检查是否为刘海屏设备
 */
export const isNotchDevice = (): boolean => {
  const info = getSafeAreaInfo()
  return info.safeAreaInsets.top > info.statusBarHeight
}

/**
 * 检查是否有底部安全区域（如iPhone X系列的Home指示器）
 */
export const hasBottomSafeArea = (): boolean => {
  const info = getSafeAreaInfo()
  return info.safeAreaInsets.bottom > 0
}

/**
 * 获取适配后的状态栏高度（包含安全距离）
 */
export const getAdaptedStatusBarHeight = (): number => {
  const info = getSafeAreaInfo()
  return Math.max(info.statusBarHeight, info.safeAreaInsets.top)
}

/**
 * 页面级别的安全距离配置
 */
export const pageConfigs = {
  // 搜索页面 - 居中布局
  search: {
    container: safeAreaClasses.fullscreen,
    centerContent: true
  },
  
  // 列表页面 - 带内边距
  list: {
    container: safeAreaClasses.withPadding('24rpx')
  },
  
  // 配置页面 - 带内边距
  config: {
    container: safeAreaClasses.withPadding('24rpx')
  },
  
  // 有底部按钮的页面
  withBottomAction: {
    container: safeAreaClasses.withPadding('24rpx'),
    bottomSpacer: calcHeightWithSafeArea('120rpx'),
    bottomActions: safeAreaClasses.fixedBottom('20rpx')
  }
}

export default {
  getSafeAreaInfo,
  getSafeAreaCSSVars,
  safeAreaClasses,
  calcHeightWithSafeArea,
  calcPaddingWithSafeArea,
  isNotchDevice,
  hasBottomSafeArea,
  getAdaptedStatusBarHeight,
  pageConfigs
}
